<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 2017/8/14
 * Time: 下午2:38
 */
class ActivityService extends Service{
	
    public static function indexHeader(){
		return array(
				'id'              => array('text' => '序号', 'rule' => 'int'),
				'server_list'       => array('text' => '同步服务器'),
				'activity_id'     => array('text' => '大活动ID'),
				'activity_name'   => array('text' => '大活动名称'),
				'classify'        => array('text' => '类别'),
				'duration'        => array('text' => '持续时间(天)'),
				'start_time'      => array('text' => '开始时间'),
				'end_time'        => array('text' => '结束时间'),
				'status_open'     => array('text' => '状态'),
				'synchro_time'    => array('text' => '同步时间'),
				'create_user'     => array('text' => '创建人'),
				'create_time'     => array('text' => '创建时间'),
				'action'          => array('text' => '操作')
		);
	}
	
    public static function indexData($params){
    	$ip = ResourceService::getIp();
		$modOssActivity = new ModOssActivity();
		$result = $modOssActivity->page($params);
		
		$classifyList = ActivityCategoryService::classifyList();
		$userList = self::userList();
		$length = count($result['data']);
		
		$user = AccAuth::me();		
		
		foreach($result['data'] as &$v){
			$type = 1;
			if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
				$type = 2;
			}
			$v['classify'] = $classifyList[$v['classify']];
			$v['create_user'] = $userList[$v['create_user']];
			$start_time = $v['start_time'];
			$end_time = $start_time + ($v['duration'] - 1) * 60 * 60 * 24;
			$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
			$v['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
			$v['end_time'] = date('Y-m-d 23:59:59', $end_time);
			$end_time = strtotime($v['end_time']);
			$status = $v['status'];
			$is_open = $v['is_open'];
			$now_time = time();
			
			$v['action'] = '';
			if($status == 1){ //未同步，同步按钮、编辑按钮
				$status = '未同步';
				$is_open = '未开启';
				$v['status_open'] = $is_open;
				$v['synchro_time'] = $status;
				
				$v['action'] .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=activity/edit').'&id='.$v['id'].'">编辑</a>  ';
				if($now_time < $start_time ){ 
					$v['action'] .= '<a href="JavaScript:void(0);" name="synchro_one" data-id="'.$v['id'].'" >同步</a>';
					$v['server_list'] = '<a href="JavaScript:void(0);" name="synchro_one" data-id="'.$v['id'].'" >同步</a>';
					//$checkbox = '<input value="'.$v['id'].'" name="id" type="checkbox">&nbsp;&nbsp;';
				}else{
					$v['server_list'] = '<font color="red">时间不符合</font>';
					//$checkbox = '<input value="'.$v['id'].'" name="id" disabled="disabled" type="checkbox">&nbsp;&nbsp;';
					
					/**测试所用**/
					//$v['server_list'] = '<a href="JavaScript:void(0);" name="synchro_one" data-id="'.$v['id'].'" >同步</a>';
					/**测试所用**/
				}				
				//$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=activity/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';	
				
			}
			if($status == 2){ //已同步	
				//$v['action'] .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=activity/edit').'&id='.$v['id'].'">编辑</a>  ';
				$v['synchro_time'] = date('Y-m-d H:i:s', $v['synchro_time']);
				if($now_time < $start_time && $is_open == 0){  //作废按钮（修改is_open为1）
					$is_open = '未开启';
					if($user['Identity'] == 2 || $user['UserID'] == 1){ //身份为identity为2才显示
						$v['action'] .= '<a href="JavaScript:void(0);" name="active_cancle_one" data-id="'.$v['id'].'" style="color:red;">作废</a>';
					}
				}
				if($start_time < $now_time && $now_time< $end_time && $is_open == 0){ //关闭按钮，关闭后变为is_open 1
					$is_open = '<font color="blue">已开启</font>';
					if($user['Identity'] == 2 || $user['UserID'] == 1){
						$v['action'] .= '<a href="JavaScript:void(0);" name="active_close_one" data-id="'.$v['id'].'" >关闭</a>';
					}
				}
				if($start_time < $now_time && $now_time< $end_time && $is_open == 1){ //开启按钮，开启后变为is_open 0
					$is_open = '<font color="green">已关闭</font>';
					$ip = ResourceService::getIp();					
					//$v['action'] .= '<a href="JavaScript:void(0);" name="active_open_one" data-id="'.$v['id'].'" >重新开启</a>';
					if($ip != '123.58.17.219'){
						$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=activity/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
					}
				}
				if($now_time > $end_time){ //删除按钮
					$is_open = '<font color="violet">已结束</font>';
				    if($ip != '123.58.17.219'){
				       $v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=activity/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
			        }
				}
				$v['status_open'] = $is_open;
				//$checkbox = '<input value="'.$v['id'].'" name="id" disabled="disabled" type="checkbox">&nbsp;&nbsp;';
				$v['server_list'] = '已同步，<a href="JavaScript:void(0);" name="synchro_info_view" data-id="'.$v['id'].'" data-url="?r=activity/synchroInfo" >查看详情</a>';
				
				/**测试所户**/
				//$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=activity/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
				/**测试所户**/
				
			}
			if($status == 3){ //已作废
				$is_open = '<font color="red">已作废</font>';
				$v['synchro_time'] = date('Y-m-d H:i:s', $v['synchro_time']);
				$v['status_open'] = $is_open;
				if($ip != '123.58.17.219'){
				    $v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=activity/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
			    }
			}
			
			if($status == 4){ //失败
				$is_open = '<font color="red">同步失败</font>';
				$v['status_open'] = $is_open;
				$v['synchro_time'] = date('Y-m-d H:i:s', $v['synchro_time']);
				$v['server_list'] = '同步失败，<a href="JavaScript:void(0);" name="synchro_info_view" data-id="'.$v['id'].'" data-url="?r=activity/synchroInfo" >查看详情</a>';
				
				//$v['action'] .= '<a href="JavaScript:void(0);" name="synchro_one" data-id="'.$v['id'].'" >重新同步</a>';
				if($ip != '123.58.17.219'){
				   $v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=activity/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
			    }
			}
			
			if($status == 5){ //部分失败
				$is_open = '<font color="red">部分失败</font>';
				$v['status_open'] = $is_open;
				$v['synchro_time'] = date('Y-m-d H:i:s', $v['synchro_time']);
				$v['server_list'] = '部分同步失败，<a href="JavaScript:void(0);" name="synchro_info_view" data-id="'.$v['id'].'" data-url="?r=activity/synchroInfo" >查看详情</a>';
				
				
				if($now_time < $start_time && $is_open == 0){  //作废按钮（修改is_open为1）
					$is_open = '未开启';
					if($user['Identity'] == 2 || $user['UserID'] == 1){
						$v['action'] .= '<a href="JavaScript:void(0);" name="active_cancle_one" data-id="'.$v['id'].'" style="color:red;">作废</a>';
					}
				}
				if($start_time < $now_time && $now_time< $end_time && $is_open == 0){ //关闭按钮，关闭后变为is_open 1
					$is_open = '<font color="blue">已开启</font>';
					if($user['Identity'] == 2 || $user['UserID'] == 1){
						$v['action'] .= '<a href="JavaScript:void(0);" name="active_close_one" data-id="'.$v['id'].'" >关闭</a>';
					}
				}
				if($start_time < $now_time && $now_time< $end_time && $is_open == 1){ //开启按钮，开启后变为is_open 0
					$is_open = '<font color="green">已关闭</font>';
					if($ip != '123.58.17.219'){
						$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=activity/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
					}					
				}
				if($now_time > $end_time){ //删除按钮
					$is_open = '<font color="violet">已结束</font>';	
					if($ip != '123.58.17.219'){
						$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=activity/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
					}				
				}
				$v['status_open'] = $v['status_open'].'('.$is_open.')';
				
			}	
			
			$checkbox = '<input value="'.$v['id'].'" data-status="'.$v['status'].'" data-tmp="'.$start_time.'" name="id" type="checkbox">&nbsp;&nbsp;';
			$v['id'] = $checkbox.$v['id'];
		}
		
		unset($v);
		return $result;
	}

	
	public static function doAdd($params){
		$start_time = strtotime($params['start_time']);
		$user = AccAuth::me();
		$modOssActivity = new ModOssActivity();
		$modOssActivity->game = $params['g'];
		$modOssActivity->category_id   = $params['category_id'];
		$modOssActivity->postpone  = $params['postpone'];
		$modOssActivity->start_time  = $start_time;
		$modOssActivity->create_user   = $user['UserID'];
		$modOssActivity->create_time   = time();
		$modOssActivity->update_user   = $user['UserID'];
		$modOssActivity->update_time   = time();
		$result = $modOssActivity->save();
		return array('state' => true, 'msg' => '成功增加活动');
	}
	
	public static function get($id){
		$modOssActivity = new ModOssActivity();
		$info = $modOssActivity->getInfo($id);
		if(count($info) == 0){
			return false;
		}else{
			return $info[0];
		}		
	}
	
	
	public static function doEdit($id, $params){
		if(empty($id)) return array('state' => false, 'msg' => '非法参数');
		$user = AccAuth::me();
		$modOssActivity = new ModOssActivity();
		$update = array();
		$start_time = strtotime($params['start_time']);
		$update['category_id'] = $params['category_id'];
		$update['postpone'] = $params['postpone'];
		$update['start_time'] = $start_time;
		$update['update_user']  = $user['UserID'];
		$update['update_time']  = time();
		$result = $modOssActivity->updateByPk($id, $update);
		return array('state' => true, 'msg' => '编辑成功');
	}
	
	
	public static function delete($id){
		if(empty($id)) return array('state' => false, 'msg' => '非法参数');
		$modOssActivity = new ModOssActivity();
		$result = $modOssActivity->deleteByPk($id);
		if($result !== false){ //删除同步信息表的数据
			$modSynchroInfo = new ModSynchroInfo();
			$modSynchroInfo->deleteAllByAttributes(array('classify'=>'activity','classify_id' => $id));
		}
		return array('state' => true, 'msg' => '成功删除');
	}
	
	public static function synchro($ids,$servers,$channel,$cls_channel){
		if($ids == '' || $servers == "" || $channel == ""){
			return array('status' => 0, 'msg' => '参数非法','data'=>'');
		}
		$modSynchroInfo = new ModSynchroInfo();
		$modOssActivity = new ModOssActivity();
		$activityList  = $modOssActivity->getActivityForIds($ids);		
		//判断是否符合同步的要求（status需为1 且 $now_time < $start_time 才能进行同步）
		$now_time = time();
		$show_flag = true;
		$info = '未同步且开始时间大于当前时间的活动才能进行同步，序号为 ';
		foreach ($activityList as $key=>$val){
			if($val['status'] != 1 || $now_time > $val['start_time']){
				/**测试所户**/
				$show_flag = false;
				/**测试所户**/
				$info .= $val['id'] . ', ';
			}
		}
		
		$info = substr($info,0,strlen($info)-2);
		$info .= ' 的活动不符合同步的条件';
		if(!$show_flag){
			$out = array('status'=>0,'msg'=>$info,'data'=>'');
			return $out;
		}
		//判断是否符合同步的要求（status需为1 且 $now_time < $start_time 才能进行同步）
		
		//exit;
		$servers_array = $channel_array = $list_s = $list_c = $ids_array = $cls_channel_array = array();
		$servers_array = explode(',', $servers);
		$channel_array = explode(',', $channel);
		$cls_channel_array = explode(',', $cls_channel);
		$ids_array = explode(',', $ids);
		
		foreach ($channel_array as $key=>$val){
			$l_array = array();
			$l_array = explode('-',$val);
			$list_c[$l_array[1]]['special_id'] = $l_array[1];
			$list_c[$l_array[1]]['channel'][$key] = $l_array[0];
		}
		
		$modServer = new ModServer();
		foreach ($servers_array as $key=>$val){
			$l_array = array();
			$l_array = explode('-',$val);
			$list_s[$l_array[1]]['special_id'] = $l_array[1];
			$list_s[$l_array[1]]['server'][$key] = $l_array[0];
			//查询每个服所带的渠道
			$channelData = $modServer->getChannel($l_array[0]);
			$cl_array = array();
			foreach ($channelData as $ck=>$cv){
				$cl_array[] = $cv['channel_id'];
			}
			$list_s[$l_array[1]]['channel'][$l_array[0]] = $cl_array;
		}
		
		foreach ($list_c as $skey=>$sval){
			if(!isset($list_s[$sval['special_id']]['server'])){
				$out = array('status'=>0,'msg'=>'存在选了渠道但不选服务器的错误','data'=>'');
				return $out;
			}
		}
		
		$server_array = array();
		foreach ($list_s as $skey=>$sval){			
			$server_array = array_merge($server_array,$list_s[$sval['special_id']]['server']);
			if(!isset($list_c[$sval['special_id']]['channel'])){
				$out = array('status'=>0,'msg'=>'存在选了服务器但不选渠道的错误','data'=>'');
				return $out;
			}
		}
		
		$server_array_ids = implode(',', $server_array);
		$serverList = PubServer::getOptions($platform, $game, 'id');
		$sinfo = '';
		$iden_flag = true;
		//同一个活动只能同步一个服务器
		foreach ($activityList as $akey=>$aval){
			$serverIden = SynchroInfoService::getServerIden($server_array_ids,'activity',$aval['activity_id']);
			if(count($serverIden) != 0){
				$iden_flag = false;
				$sinfo .= '服务器 ';
				foreach ($serverIden as $sk=>$sv){
					$sinfo .= ' <font color="red">'.$serverList[$sk].' </font>';
				}
				$sinfo .= '已经同步了<font color="red">' .$aval['activity_name'].'('.$aval['activity_id'].')</font> 这个活动<br/>';
			}	
		}
		
		if(!$iden_flag){
			$out = array('status'=>0,'msg'=>$sinfo,'data'=>'');
			return $out;
		}
		
		//exit;
		
		$synchroInfoArray = array();		
		$info_res = '';
		foreach ($activityList as $akey=>$aval){
			$params = array();
			$params['activity_id'] = $aval['activity_id'];
			$params['activity_name'] = $aval['activity_name'];
			$params['classify'] = $aval['classify'];
			$params['duration'] = $aval['duration'];
			$params['start_time'] = $aval['start_time'];
			$params['postpone'] = $aval['postpone'];
			$params['is_open'] = 0;
			//$is_all_success = 1; //是否全部成功，1全部成功，0部分失败或者全部失败
			$all_i = 0; //总次数
			$success_i = 0; //成功次数
			$fail_i = 0; //失败次数
			foreach ($list_s as $skey=>$sval){				
				$special_id = $sval['special_id'];
				$channel = $list_c[$sval['special_id']]['channel'];  //选中的渠道
				foreach ($sval['server'] as $sskey=>$ssval){
					$all_i ++;
					$server_id = $ssval;
					//判断是否全渠道
					if(in_array($special_id, $cls_channel_array)){
						$channel_ids = '';
					}else{
						//取选中的渠道和本身渠道的合集
						$channel = array_intersect($channel,$list_s[$special_id]['channel'][$server_id]);
						$channel_ids = implode(',', $channel);
					}
					$params['channel_ids'] = $channel_ids;
					$params['server_id'] = $server_id;
					$s_result = self::send($params);
// 					if($all_i == 2){
// 						$s_result['state'] = false;
// 					}else{
// 						$s_result['state'] = true;
// 					}
			        
					if($s_result['state']){
						$success_i++;
						$status = 1;
						$info_res .= '序号为 <font color="red">'.$aval['id'].'</font> 的活动发送服务器 <font color="red">'.$serverList[$server_id].' </font>成功<br/>';
					}else{
						$fail_i++;
						//$is_all_success = 0;
						$status = 2;
						$info_res .= '序号为 <font color="red">'.$aval['id'].'</font> 的活动发送服务器 <font color="red">'.$serverList[$server_id].' </font>失败，失败原因'.$s_result['msg'].'<br/>';
					}
					
					$d = array();
					$d['classify'] = 'activity';
					$d['classify_id'] = $aval['id'];
					$d['special_id'] = $special_id;
					$d['server_id'] = $server_id;
					$d['channel_ids'] = $channel_ids;
					$d['status'] = $status;
					$d['info'] = $s_result['msg'];
					$d['classify_iden'] = $aval['activity_id'];
					array_push($synchroInfoArray, $d);
				}
			}
			
			if($fail_i>0 && $fail_i<$all_i){ //部分失败
				$modOssActivity->updateByPk($aval['id'],array('is_open'=>0,'synchro_time'=>time(),'status'=>5));
			}else if($fail_i>0 && $fail_i=$all_i){ //全部失败
				$modOssActivity->updateByPk($aval['id'],array('synchro_time'=>time(),'status'=>4));
			}else{ //全部成功
				$modOssActivity->updateByPk($aval['id'],array('is_open'=>0,'synchro_time'=>time(),'status'=>2));
			}
			
// 			if($is_all_success == 0){  //失败
// 				$modOssActivity->updateByPk($aval['id'],array('synchro_time'=>time(),'status'=>4));
// 			}else{
// 				$modOssActivity->updateByPk($aval['id'],array('is_open'=>0,'synchro_time'=>time(),'status'=>2)); //修改	
// 			}
		}
// 		$modOssActivity->updateByPk($ids_array,array('is_open'=>0,'synchro_time'=>time(),'status'=>2)); //修改
		
		$modSynchroInfo->addmuch($synchroInfoArray);  //记录同步信息
		
		return array('status' => 1, 'msg' => $info_res,'data'=>'');	
	}
	
	public static function send($params){		
		$api = new ApiService($params['server_id']);
		unset($params['server_id']);
		if($params['is_open'] == 0){
			$params['is_open'] = 1;  //开启
		}else{
			$params['is_open'] = 0;  //关闭
		}
		
		if($params['channel_ids'] == ''){
			$params['channel_ids'] = '{}';	//全渠道		
		}else{
		    $modChannel = new ModChannel();
			$channelList = $modChannel->getChannelForIds($params['channel_ids']);
			$tabs = '';
			foreach ($channelList as $key=>$val){
				$tabs .= ','.$val['tab'];	
			}		
			$tabs =  substr($tabs, 1);
			$params['channel_ids'] = '{'.$tabs.'}';
		}
		
		return $api->sendActivity($params);
	}
	
	
	public static function updateStatus($ids,$type){
		$modOssActivity = new ModOssActivity();
		$modSynchroInfo = new ModSynchroInfo();
		$activityList  = $modOssActivity->getActivityForIds($ids);
		$now_time = time();
		$show_flag = true;
		$type_info = '';
		if($type == 2){ //作废（status为2且$now_time < $start_time $is_open == 0）

			$info = '已同步且开始时间大于当前时间的活动才能进行作废，序号为 ';
			foreach ($activityList as $key=>$val){
				if($val['status'] != 2 && $val['status'] != 5 || $now_time > $val['start_time'] || $val['is_open'] != 0){
					$show_flag = false;
					$info .= $val['id'] . ', ';
				}
			}
			
			$info = substr($info,0,strlen($info)-2);
			$info .= ' 的活动不符合作废的条件';
			if(!$show_flag){
				$out = array('status'=>0,'msg'=>$info,'data'=>'');
				return $out;
			}
			$is_open = 1;
			$type_info = '作废';
			$sys_status = 3;
			$status = 3;
			
		}else if($type == 1){ //关闭(status为2且$start_time < $now_time && $now_time< $end_time && $is_open == 0）
			$info = '已同步、当前时间位于开始时间和结束时间之间、已开启的活动才能进行关闭，序号为 ';
			foreach ($activityList as $key=>$val){
				$end_time = $val['start_time'] + ($val['duration'] - 1) * 60 * 60 * 24;
				$d_end_time = date('Y-m-d 23:59:59', $end_time);
				$end_time = strtotime($d_end_time);				
				if($val['status'] != 2 && $val['status'] != 5 || $now_time < $val['start_time'] || $now_time > $end_time || $val['is_open'] != 0){
					$show_flag = false;
					$info .= $val['id'] . ', ';
				}
			}
				
			$info = substr($info,0,strlen($info)-2);
			$info .= ' 的活动不符合关闭的条件';
			if(!$show_flag){
				$out = array('status'=>0,'msg'=>$info,'data'=>'');
				return $out;
			}
			
			$is_open = 1;
			$type_info = '关闭';
			$sys_status = 4;
			$status = 2;
		}else if($type == 0){ //开启（status为2且 $start_time < $now_time && $now_time< $end_time && $is_open == 1）
			$info = '已同步、当前时间位于开始时间和结束时间之间、已关闭的活动才能进行开启，序号为 ';
			foreach ($activityList as $key=>$val){
				$end_time = $val['start_time'] + ($val['duration'] - 1) * 60 * 60 * 24;
				$d_end_time = date('Y-m-d 23:59:59', $end_time);
				$end_time = strtotime($d_end_time);
				if($val['status'] != 2 && $val['status'] != 5 || $now_time < $val['start_time'] || $now_time > $end_time || $val['is_open'] != 1){
					$show_flag = false;
					$info .= $val['id'] . ', ';
				}
			}
			
			$info = substr($info,0,strlen($info)-2);
			$info .= ' 的活动不符合开启的条件';
			if(!$show_flag){
				$out = array('status'=>0,'msg'=>$info,'data'=>'');
				return $out;
			}
			
			$is_open = 0;
			$type_info = '开启';
			$sys_status = 5;
			$status = 2;
		}else{
			return array('status' => 0, 'msg' => '参数非法','data'=>'');
		}
		
		$info_res = '';
		$serverList = PubServer::getOptions($platform, $game, 'id');
		
		foreach ($activityList as $key=>$val){
			//获取需要的同步的服务器和渠道
			$SynchroInfoList = SynchroInfoService::getSynchroInfo('activity', $val['id']);
			$is_all_success = 1; //是否全部成功，1全部成功，0部分失败或者全部失败
			foreach ($SynchroInfoList as $skey=>$sval){
				if(($type == 1 || $type == 2) &&  $sval['is_open'] == 1){ //关闭或者作废功能
					continue;
				}
				if($type == 0 && $sval['is_open'] == 0){ //重新开启
					continue;
				}
				$params = array();
				$params['channel_ids'] = $sval['channel_ids'];
				$params['server_id'] = $sval['server_id'];
				$params['is_open'] = $is_open;
				$params['activity_id'] = $val['activity_id'];
				
				$params['activity_name'] = $val['activity_name'];
				$params['classify'] = $val['classify'];
				$params['duration'] = $val['duration'];
				$params['start_time'] = $val['start_time'];
				$params['postpone'] = $val['postpone'];
				
				$s_result = self::send($params);
				//$s_result['state'] = true;
				//$s_result['msg'] = '';
				if($s_result['state']){				
					$info_res .= '序号为 <font color="red">'.$val['id'].'</font> 的活动 '.$type_info.' <font color="red">'.$serverList[$sval['server_id']].' </font>成功<br/>';
					$modSynchroInfo->updateByPk($sval['id'],array('status'=>1,'info'=>'成功:'.$s_result['msg'],'is_open'=>$is_open));
				}else{
					$is_all_success = 0;
					$info_res .= '序号为 <font color="red">'.$val['id'].'</font> 的活动 '.$type_info.' <font color="red">'.$serverList[$sval['server_id']].' </font>失败，失败原因'.$s_result['msg'].'<br/>';
					$modSynchroInfo->updateByPk($sval['id'],array('status'=>$sys_status,'info'=>'失败:'.$s_result['msg'])); //修改
				}
			}
			
			if($is_all_success == 0){  //失败
				$modOssActivity->updateByPk($val['id'],array('update_time'=>time(),'status'=>$status)); //修改
			}else{
				$modOssActivity->updateByPk($val['id'],array('is_open'=>$is_open,'update_time'=>time(),'status'=>$status)); //修改
			}
			
		}

		return array('status' => 1, 'msg' => $info_res,'data'=>'');
		
	}

	
	/*
	 * 去重
	 */
	public static function repeat($id = 0, $field, $val,$g){
		$modOssActivity = new ModOssActivity();
		$conut = $modOssActivity->repeat($id, $field, $val,$g);
		return $conut;
	}
	
	/**
	 * 同步信息
	 * @param unknown $id
	 */
	public static function synchroInfo($id){
		$ip = ResourceService::getIp();
		$game = PubBase::getCurrentGame();
		$serverList = PubServer::getOptions($platform, $game, 'id');
		$channelList = ChannelService::getChannel(array('g'=>$game));
		$specialList = SpecialServerService::specialList();
		
		$activityInfo = self::get($id);
		$start_time = $activityInfo['start_time'];
		$end_time = $start_time + ($activityInfo['duration'] - 1) * 60 * 60 * 24;
		$end_time = strtotime( date('Y-m-d 23:59:59', $end_time));
		$now_time = time();
		
		$user = AccAuth::me();
		
		$SynchroInfoList = SynchroInfoService::getSynchroInfo('activity', $id,false);
		$server_list = array();
		foreach ($SynchroInfoList as $key=>$val){
			if($val['channel_ids'] == ''){
				$channels = '全渠道';
			}else{
				$channels_array = explode(',', $val['channel_ids']);
				$channels = '';
				foreach ($channels_array as $ck=>$cv){
					$channels .= ','.$channelList[$cv];
				}
				$channels =  substr($channels, 1);
			}
			
				
			$status = '';
			if($val['status'] == 2){
				$status = '(同步失败)';
			}
			if($val['status'] == 3){
				$status = '(作废失败)';
			}
			if($val['status'] == 4){
				$status = '(关闭失败)';
			}
			if($val['status'] == 5){
				$status = '(重新开启失败)';
			}
			$d = array();
			$d['server_list'] = $specialList[$val['special_id']].'--'.$serverList[$val['server_id']].'--'.$channels;
			$d['status'] = $val['status'];
			$d['status_text'] = $status;
			$is_open = $val['is_open'];
			
			
			$action = '';
			if($now_time < $start_time && $is_open == 0 && $val['status'] != 2){  //作废按钮
				if($user['Identity'] == 2 || $user['UserID'] == 1){
					$action = '<a href="JavaScript:void(0);" name="active_cancle_one" data-id="'.$val['id'].'" style="color:red;">作废</a>';
				}
			}
			if($start_time < $now_time && $now_time< $end_time && $is_open == 0 && $val['status'] != 2){ //关闭按钮
				if($user['Identity'] == 2 || $user['UserID'] == 1){
					$action = '<a href="JavaScript:void(0);" name="active_close_one" data-id="'.$val['id'].'" >关闭</a>';
				}
			}
			
			$open = '';
			if($now_time < $start_time && $is_open == 1 && $val['status'] != 2){  
				$open = '已作废';
			}
			if($start_time < $now_time && $now_time< $end_time && $is_open == 1 && $val['status'] != 2){ 
				$open = '已关闭';				
			}
			
			$d['action'] = $action;
			$d['open'] = $open;
			array_push($server_list, $d);
		}
		return $server_list;
	}
	
	public static function info_update($id,$type){
		if($type == 2){ //作废
			$is_open = 1;
			$type_info = '作废';
			$sys_status = 3;
			$status = 3;
				
		}else if($type == 1){ //关闭
			$is_open = 1;
			$type_info = '关闭';
			$sys_status = 4;
			$status = 2;
		}
		
		$info = SynchroInfoService::get($id);
		
		$modActivityCategory = new ModActivityCategory();
		$cat_obj = $modActivityCategory->findByAttributes(array('activity_id'=>$info['classify_iden']));
		
		$modOssActivity = new ModOssActivity();
		$act_obj = $modOssActivity->findByAttributes(array('id'=>$info['classify_id']));
		
		if(!$info){
			return array('status' => 0, 'msg' => '参数非法','data'=>'');
		}
		$modSynchroInfo = new ModSynchroInfo();
		$params = array();
		$params['channel_ids'] = $info['channel_ids'];
		$params['server_id'] = $info['server_id'];
		$params['is_open'] = $is_open;
		$params['activity_id'] = $info['classify_iden'];
		
		$params['activity_name'] = $cat_obj->activity_name;
		$params['classify'] = $cat_obj->classify;
		$params['duration'] = $cat_obj->duration;
		
		$params['start_time'] = $act_obj->start_time;
		$params['postpone'] = $act_obj->postpone;

		$s_result = self::send($params);
		//$s_result['state'] = true;
		if($s_result['state']){	
			$info_res = $type_info.'成功:'.$s_result['msg'];
			$modSynchroInfo->updateByPk($id,array('status'=>1,'info'=>$type_info.'成功:'.$s_result['msg'],'is_open'=>1));
		}else{	
			$info_res = $type_info.'失败:'.$s_result['msg'];		
			$modSynchroInfo->updateByPk($id,array('status'=>$sys_status,'info'=>$type_info.'失败:'.$s_result['msg'])); //修改
		}
		
		//查询所属活动下的服务器是否全都
		$synList = SynchroInfoService::getSynchroInfo('activity',$info['classify_id'],true);		
	    $syn_l = count($synList);
	    $is_open_l = 0;
		foreach ($synList as $key=>$val){
			if($val['is_open'] == 1){
				$is_open_l++;
			}
		}
		
		if($syn_l == $is_open_l){ //全部为关闭
			$modOssActivity = new ModOssActivity();
			$modOssActivity->updateByPk($info['classify_id'],array('is_open'=>1,'status'=>$status)); //修改
		}
		
		return array('status' => 1, 'msg' => $info_res,'data'=>'');
		
	}
    

}